package com.lecyon.farm.controller;

import com.lecyon.farm.base.BaseRequestVo;
import com.lecyon.farm.base.BaseResponse;
import com.lecyon.farm.base.RestResponse;
import com.lecyon.farm.comment.JwtToken;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.service.impl.AuthenticationService;
import com.lecyon.farm.util.ResultUtil;
import com.lecyon.farm.vo.query.QueryJsSysUserVo;
import com.lecyon.farm.vo.request.LoginVo;
import com.lecyon.farm.vo.request.LogoutVo;
import com.lecyon.farm.vo.view.JsSysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author Yao Zheng
 * @Date 2019/9/5 17:20
 */
@RestController
@Api(tags = "Shiro权限认证")
@ResponseBody
@CrossOrigin
@Setter
public class AuthenticationController {

    private AuthenticationService authenticationService;

    @Autowired
    public AuthenticationController(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    @ApiOperation(value = "登录并获取token", notes = "登录并获取token，author:YaoZheng")
    @PostMapping("/login")
    public RestResponse<JwtToken> login(@RequestBody @Valid LoginVo loginVo) {
        return authenticationService.login(loginVo);
    }

    @ApiOperation(value = "登出", notes = "登出，author:YaoZheng")
    @PostMapping("/logout")
    public BaseResponse logout(@RequestBody @Valid LogoutVo logoutVo) {
        return authenticationService.logout(logoutVo);
    }

    @ApiOperation(value = "查找用户信息", notes = "查找用户信息，author:YaoZheng")
    @PostMapping("/getUserInfo")
    public RestResponse<JsSysUserVo> getUserInfoByToken(@RequestBody @Valid BaseRequestVo<QueryJsSysUserVo> vo) {
        QueryJsSysUserVo queryCondition = vo.getEntity(QueryJsSysUserVo.class);
        queryCondition.setUserToken(vo.getUserToken());
        return authenticationService.getUserInfoByToken(queryCondition);
    }

    /**
     * 修改登录密码
     *
     * @return
     */
//    @PostMapping("/password/put")
//    public BaseResponse modifyPassword(@RequestBody @Valid ModifyLoginPassword vo) {
//        return authenticationService.changePassword(vo);
//    }

    /**
     * 重置登录密码，用于忘记密码的情况
     *
     * @return
     */
//    @RequiresRoles(value = {IahsSysConstant.SYS_MASTER_NAME, IahsSysConstant.ADMINISTRATOR_NAME}, logical = Logical.OR)
//    @PostMapping("/password/reset")
//    public BaseResponse resetPassword(@RequestBody @Valid ResetLoginPassword vo) {
//        return authenticationService.resetPassword(vo);
//    }

    /**
     * 用于权限验证失败时的跳转
     *
     * @param request
     * @return
     */
    @PostMapping("/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public RestResponse unauthorized(HttpServletRequest request) {
        return ResultUtil.error(null, CommonConstant.EX_CLIENT_FORBIDDEN, CommonConstant.EX_AUTHORITY_ERROR_MSG);
    }

}
